package m.jclecai.com.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.RedPkgDetailAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.AccountBaseBean;
import m.jclecai.com.bean.AccountParticularsBean;
import m.jclecai.com.bean.GroupBuyBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/6/27.
 */

public class RedPkgDetailActivity extends BaseActivity implements
        PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener {

    @BindView(R.id.pl_list)
    PullToRefreshListView plList;
    RedPkgDetailAdapter adapter;
    List<AccountParticularsBean> lists;
    String flag = "";

    @Override
    protected int getContentView() {
        return R.layout.activity_red_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        leftOptionText("");
        setTitle("红包使用明细");
        showLoadingLayout(plList);
    }

    @Override
    protected void initData() {
        super.initData();
        flag = getIntent().getStringExtra("flag");
        lists = new ArrayList<>();
        adapter = new RedPkgDetailAdapter(this, lists);
        plList.setAdapter(adapter);
        plList.setOnRefreshListener(this);
        //plList.setOnItemClickListener(this);
        getRedPkgDetail();
    }

    @Override
    protected void resetWindow() {

    }

    /**
     *
     */
    void getRedPkgDetail() {
        Map<String, String> pair = new HashMap<>();
        pair.put("user", PreferenceUtil.getUserName(this));//Yes	string	用户名
        pair.put("password", PreferenceUtil.getPassword(this));//Yes	string	密码
        pair.put("flag", flag);//
        HttpManager.postAsyn(Constant_Url.MY_REDPACKET_DETAIL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
                showInternetOffLayout();
                if (plList != null)
                    plList.onRefreshComplete();
            }

            @Override
            public void onResponse(String response) {
                if (plList != null)
                    plList.onRefreshComplete();
                closeLoadingLayout();
                AccountBaseBean body = FastOk.get(response, AccountBaseBean.class);
                if (body != null && "0".equals(body.getStatus())) {
                    lists.addAll(body.getRecords());
                    adapter.notifyDataSetChanged();
                }
                if (lists.size() <= 0)
                    showEmptyLayout();
            }
        }, pair);

    }

    @Override
    public void OnRetry(String btnMessage) {
        super.OnRetry(btnMessage);
        lists.clear();
        showLoadingLayout(plList);
        getRedPkgDetail();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        lists.clear();
        getRedPkgDetail();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AccountParticularsBean bean = lists.get(position - 1);
        GroupBuyBean gb = new GroupBuyBean(bean.getPid(), bean.getProjectno());
        startActivity(new Intent(RedPkgDetailActivity.this, BuyDetailActivity.class)
                .putExtra("bean", JSON.toJSONString(gb)));
    }
}
